/*
 * main.c
 *
 *  Created on: Jul 31, 2013
 *      Author: lxzhu
 */
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

int windowIds[] = { 0, 0 };
int displayListIds[] = { 1, 2 };
GLboolean swapWindow = GL_FALSE;
void onDrawLeft();
void onDrawRight();
void createLeftDisplayList();
void createRightDisplayList();
void onMouse(int button, int state, int x, int y);

int main(int argc, char** argv) {
	glutInit(&argc, argv);

	glutInitWindowPosition(0, 0);
	//glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(300, 400);
	windowIds[0] = glutCreateWindow("LeftWindow");
	glutDisplayFunc(onDrawLeft);
	glutMouseFunc(onMouse);
	createLeftDisplayList();

	glutInitWindowPosition(300, 0);
	glutInitWindowSize(300, 400);
	windowIds[1] = glutCreateWindow("RightWindow");
	glutDisplayFunc(onDrawRight);
	glutMouseFunc(onMouse);
	createRightDisplayList();
	glutMainLoop();
	return 0;
}

void createLeftDisplayList() {
	glNewList(displayListIds[0], GL_COMPILE);
	glClear(GL_COLOR_BUFFER_BIT);
	glBegin(GL_POLYGON);
	glVertex2f(-0.5, -0.5);
	glVertex2f(0.5, -0.5);
	glVertex2f(0, 0.5);
	glEnd();
	glEndList();

}

void createRightDisplayList() {
	glNewList(displayListIds[1], GL_COMPILE);
	glClear(GL_COLOR_BUFFER_BIT);
	glBegin(GL_POLYGON);
	glVertex2f(-0.5, -0.5);
	glVertex2f(0.5, -0.5);
	glVertex2f(0.5, 0.5);
	glVertex2f(-0.5, 0.5);
	glEnd();
	glEndList();
}

void onDrawLeft() {
	glCallList(displayListIds[0]);
	glFlush();
}

void onDrawRight() {
	glCallList(displayListIds[1]);
	glFlush();
}

void onMouse(int button, int state, int x, int y) {
	if (swapWindow)
		swapWindow = GL_FALSE;
	else
		swapWindow = GL_TRUE;
}

